<?php
namespace Admin\Controller;
use Think\Controller;

class DemoController extends Controller {
	
	/**
	 * 乐观锁并发控制（测试）
	 * http://127.0.0.1/pay_api/index.php/Mer/demo/test?id=1&score=10
	 * ab -n2 -c2 http://127.0.0.1/pay_api/index.php/Mer/demo/test
	 * ab -n10 -c10 http://192.168.0.152/test/index.php/demo/test
	 * ab -n10 -c10 http://127.0.0.1/test/index.php/demo/test
	 * http://192.168.0.152/index.php/demo/test
	 * ab -n10 -c10 http://192.168.0.152/index.php/demo/test
	 */
	public function test() {
		$id = 1;    //   用户id
		$score = 10;   // 用户的积分
	
		$Model = M('user1', 'test_');
		$user_info = $Model->where(array('id'=>$id))->find();
		$version = $user_info['version'];   // 版本号
		if($version==1) exit('failed');
	
		$data = array('score'=>array("exp", "score+{$score}"), 'version'=>array("exp", "version+1"));
	
		// 乐观锁的核心思想：更新数据时，加入版本号version条件
		$res = $Model->where(array('id'=>$id))->save($data);  // ,'version'=>$version
		echo 'result:'.$res.'--sql:'.$Model->getLastSql();
	}
	
}

